package com.szsky.sky.service.impl.app;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szsky.sky.entity.UmcUsers;
import com.szsky.sky.entity.app.UmcAppDeviceToken;
import com.szsky.sky.mapper.UmcUserMapper;
import com.szsky.sky.mapper.app.UmcAppDeviceTokenMapper;
import com.szsky.sky.service.app.UmcAppDeviceTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UmcAppDeviceTokenServiceImpl extends ServiceImpl<UmcAppDeviceTokenMapper, UmcAppDeviceToken> implements UmcAppDeviceTokenService {

    @Resource
    private UmcAppDeviceTokenMapper umcAppDeviceTokenMapper;
    @Resource
    private UmcUserMapper UmcUserMapper;

    @Override
    public UmcAppDeviceToken saveDeviceToken(UmcAppDeviceToken appDeviceToken) {
        LambdaUpdateWrapper<UmcUsers> queryLam = new LambdaUpdateWrapper<>();
        queryLam.eq(UmcUsers::getUserId, appDeviceToken.getAccount());
        queryLam.eq(UmcUsers::getStatus, 11);
        List<UmcUsers> list = UmcUserMapper.selectList(queryLam);
        LambdaUpdateWrapper<UmcAppDeviceToken> lam = new LambdaUpdateWrapper<>();
        lam.eq(UmcAppDeviceToken::getUcUid, list.get(0).getUcUid());
        umcAppDeviceTokenMapper.delete(lam);
        appDeviceToken.setUcUid(list.get(0).getUcUid());
        umcAppDeviceTokenMapper.insert(appDeviceToken);
        return appDeviceToken;
    }


    @Override
    public List<String> queryDeviceToken(List<String> receiveAccountList) {
        LambdaUpdateWrapper<UmcAppDeviceToken> queryLam = new LambdaUpdateWrapper<>();
        queryLam.in(UmcAppDeviceToken::getAccount, receiveAccountList);
        List<UmcAppDeviceToken> list = umcAppDeviceTokenMapper.selectList(queryLam);
        if (CollectionUtil.isNotEmpty(list)) {
            List<String> tokenList = list.stream().map(UmcAppDeviceToken::getDeviceToken).collect(Collectors.toList());
            return tokenList;
        }
        return null;
    }
}
